﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolCode.Entities;
using CoolCode.Services;
using CoolCode.Web;
using CoolCode.Linq;
using CoolCode.WebServices;

namespace CoolCode.Web.UI.Views.IC
{
    public partial class Products : SearchPage<Inspection, IInspectionService> //AuthPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            handleEvents();
            if (!IsPostBack)
            {
                //BindData_Master(new PageParam(0, PageSize));
            }
        }

        public override IQueryBuilder<Inspection> CreateFilter()
        {
            return productFilter.CreateFilter().Custom(c => c.Status == ProductStatus.Out.GetAttachedString()); 
        }

        protected void handleEvents()
        {
            this.Setup(lnkSearch, pgrMaster, c =>
            {
                var list = c.Result;
                gvMaster.DataSource = list;
                gvMaster.DataBind();
                pgrMaster.Bind(list);
                udpHeader.Update();
            },
            c =>
            {
                udpHeader.Alert(c.Exception);
            });

            //pgrMaster.PageIndexChanging += (sender, e) => this.ProvideData(new PageParam(e.NewPageIndex, this.PageSize));             

            this.Setup(exporter, new ExportService());

        }

        //private IInspectionService _Service;
        //public IInspectionService Service
        //{
        //    get
        //    {
        //        if (_Service == null)
        //        {
        //            _Service = ServiceFactory.Produce<IInspectionService>();
        //        }
        //        return _Service;
        //    }
        //}

        //private const int PageSize = 10;

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    handleEvents();
        //    if (!IsPostBack)
        //    {
        //        BindData_Master(new PageParam(0, PageSize));
        //    }
        //}

        //protected void handleEvents()
        //{
        //    pgrMaster.PageIndexChanging += new GridViewPageEventHandler(pgrMaster_PageIndexChanging);
        //    lnkSearch.Click += new EventHandler(lnkSearch_Click);
        //    exporter.Setup(new ExportService(), () =>
        //        {
        //            var qb = productFilter.CreateFilter();
        //            var list = Service.List(qb);
        //            return list;
        //        });
        //    //lnkExcel.Click += new EventHandler(lnkExcel_Click);
        //    //lnkExcel.AsExportable(new ExportService(), () =>
        //    //    {
        //    //        var qb = productFilter.CreateFilter().Custom(c => c.Status == ProductStatus.Out.GetAttachedString());
        //    //        var list = Service.ListInspection(qb);
        //    //        return list;
        //    //    },
        //    //    c => ViewHelper.BindFilePath(lnkExcelSrc, c.FilePath),
        //    //    c => udpHeader.Alert(c.Message)
        //    //);
        //}

        ////protected  void lnkExcel_Click(object sender, EventArgs e)
        //// {
        ////     var qb = productFilter.CreateFilter().Custom(c => c.Status == ProductStatus.Out.GetAttachedString());  
        ////     var list = Service.ListInspection(qb );

        ////     ExportService service = new ExportService();
        ////     ExportResult message;
        ////     string savePath = service.Export (list, out message);
        ////     if (message != ExportResult.Success)
        ////     {
        ////         udpHeader.Alert(message.GetAttachedString());
        ////     }
        ////     else
        ////     {
        ////         ViewHelper.BindFilePath(lnkExcelSrc, savePath);
        ////     }
        //// }


        //protected void lnkSearch_Click(object sender, EventArgs e)
        //{
        //    BindData_Master(new PageParam(0, PageSize));
        //    udpHeader.Update();
        //}

        //private void BindData_Master(PageParam p)
        //{
        //    var qb = productFilter.CreateFilter().Custom(c => c.Status == ProductStatus.Out.GetAttachedString());
        //    var list = Service.Paging(qb, p);
        //    gvMaster.DataSource = list;
        //    gvMaster.DataBind();
        //    pgrMaster.Bind(list);
        //}


        ////分页控件的分页事件
        //protected void pgrMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    BindData_Master(new PageParam(e.NewPageIndex, PageSize));
        //}
    }
}
